﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using DotNetNinjaLibrary.Pathfinding;

namespace DotNetNinjaLibrary.TestApplication
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		private Hero hero;
		private IList<Unit> units;
		private MapModel map;
		private DateTime startTime;

		private GameController controller;

		public MainWindow()
		{
			InitializeComponent();
			units = new ObservableCollection<Unit>();
			map = new MapModel(200, 200, units);
			new Wall(new Basics.Size(10, 1)).PlaceOnMap(map, new Basics.Point(50, 10));
			new Wall(new Basics.Size(10, 10)).PlaceOnMap(map, new Basics.Point(30, 30));
			new Wall(new Basics.Size(1, 10)).PlaceOnMap(map, new Basics.Point(10, 30));
			hero = new Hero(new Basics.Size(1, 1)) { Speed = TimeSpan.FromMilliseconds(50).Ticks };
			hero.PlaceOnMap(map, new Basics.Point(7, 7));
			new BadGuy(new Basics.Size(1, 1)) { HeroToTrack = hero }.PlaceOnMap(map, new Basics.Point(50, 50));
			this.control.ItemsSource = units;
			this.control.Map = map;
			this.startTime = DateTime.Now;

			this.controller = new GameController { Map = map, Units = units };
			
			var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(33) };
			timer.Tick += new EventHandler(timer_Tick);

			controller.Initialize((DateTime.Now - this.startTime).Ticks);
			timer.Start();
		}

		private void timer_Tick(object sender, EventArgs e)
		{
			controller.Update((DateTime.Now - this.startTime).Ticks);
		}

		private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			var mousePosition = e.GetPosition(this);
			var destination = new Basics.Point((int)(mousePosition.X / 5), (int)(mousePosition.Y / 5));
			hero.GoTo(destination);
		}

		private Point startPoint;

		private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
		{
			this.startPoint = e.GetPosition(this);
			this.LostMouseCapture += new MouseEventHandler(MainWindow_LostMouseCapture);
			this.CaptureMouse();
		}

		private void MainWindow_LostMouseCapture(object sender, MouseEventArgs e)
		{
			var endPoint = e.GetPosition(this);
			var left = Math.Min(endPoint.X, startPoint.X) / 5;
			var right = Math.Max(endPoint.X, startPoint.X) / 5;

			var top = Math.Min(endPoint.Y, startPoint.Y) / 5;
			var bottom = Math.Max(endPoint.Y, startPoint.Y) / 5;

			new Wall(new Basics.Size((int)(Math.Floor(right - left)), (int)(Math.Floor(bottom - top))))
				.PlaceOnMap(map, new Basics.Point((int)left, (int)top));
		}

		private void Window_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
		{
			this.ReleaseMouseCapture();
		}
	}
}